package com.intco.ait.quotation.api.starter.advice;

import com.intco.ait.quotation.api.common.model.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author mxy
 */
@ControllerAdvice
@Slf4j
@ResponseBody
public class CommonExceptionAdvice {

    /**
     * 统一异常处理方法，@ExceptionHandler(RuntimeException.class)声明这个方法处理RuntimeException这样的异常
     *
     * @param e 捕获到的异常
     * @return 返回给页面的状态码和信息
     */
    @ExceptionHandler(Exception.class)
    public BaseResponse<String> handleLyException(Exception e) {
        if (e instanceof LocalException) {
            return BaseResponse.fail(((LocalException) e).getStatus(), e.getMessage());
        }
        log.error("[线程标识:{}] 捕获异常信息:{}", ThreadLogContext.getThreadLogId(), e.getMessage(), e);
        return BaseResponse.fail("系统异常");
    }
}